package com.software.frontend.firstPage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.software.frontend.R;
import com.software.frontend.firstPage.entity.Post;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class RecAdapter extends RecyclerView.Adapter<RecAdapter.MyVideoHolder> {
    private View view;
    private Context mContext;
    private List<Post> posts = new ArrayList<>();

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    //定义监听的接口
    private OnRecyclerItemClickListener onRecyclerItemClickListener;

    //定义一个方法可以直接提供调用
    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener onRecyclerItemClickListener){
        this.onRecyclerItemClickListener=onRecyclerItemClickListener;
    }

    public RecAdapter(Context mContext, List<Post> posts) {
        this.mContext = mContext;
        this.posts = posts;
    }

    //创建列表组件
    @NonNull
    @Override
    public RecAdapter.MyVideoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.recycleview_you_you_j_ian, parent, false);
        return new MyVideoHolder(view);
    }

    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull RecAdapter.MyVideoHolder holder, int position) {
        Glide.with(mContext)
                .load(posts.get(position).getCoverUrl())
                .into(holder.imageView);
        holder.textView.setText(posts.get(position).getLikeCount() + "万");
    }

    //返回列表数据总数
    @Override
    public int getItemCount() {
        return posts.size();
    }

    public class MyVideoHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;
        TextView textView;

        public MyVideoHolder(@NonNull View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.rec_item_image);
            this.textView = itemView.findViewById(R.id.rec_item_count);

            //对itemView监听
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onRecyclerItemClickListener != null){
                        //可以发现，点击后获取的是某个数据的Position，那么自然可以找到对应的cards信息
                        onRecyclerItemClickListener.onItemClick(getAdapterPosition(),posts);
                    }
                }
            });
        }
    }
}

